import { createStore, applyMiddleware, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import { SETNUM, SETSONG, PLAYSONG, SONGFLAG, SONGINGFLAG, SONGTYPE } from './types.js'
const data = {
  num: 1, // 测试
  song: [], // 播放歌单
  playSong: null, // 正在播放
  songFlag: false, // 播放开关
  songingflag: false, // 主播放器控制弹出
  songType: Number(localStorage.getItem('songType')) || 1, // 播放类型
}

const reducer = (state = data, action) =>{
    console.log(action)
    switch(action.type) {
        case SETNUM:
          return setnum(state, action);
        case SETSONG:
          return setSong(state, action);
        case PLAYSONG:
          return playSong(state, action);
        case SONGFLAG:
          return songFlag(state, action);
        case SONGINGFLAG:
          return songingflag(state, action);
        case SONGTYPE:
          return setsongType(state, action);
        default:
          return state
    }
}

function setnum(state, data) {
    return {
        ...state,
        num: data.data
    }
}
function setSong(state, data) {
  return {
      ...state,
      song: data.data
  }
}
function songFlag(state, data) {
  return {
    ...state,
    songFlag: data.data
  }
}
function playSong(state, data) {
  return {
    ...state,
    playSong: data.data
  }
}
function songingflag(state, data) {
  return {
    ...state,
    songingflag: data.data
  }
}
function setsongType(state, data) {
  return {
      ...state,
      songType: data.data
  }
}
const store = createStore(combineReducers({reducer}),applyMiddleware(thunk));//内部会第一次调用reducer函数，得到初始state

export default store